package com.wangsh.userscenter.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.wangsh.userscenter.common.controller.BaseController;
import com.wangsh.userscenter.common.util.ConstatFinalUtil;
import com.wangsh.userscenter.common.util.PageInfoUtil;
import com.wangsh.userscenter.users.pojo.ARole;
import com.wangsh.userscenter.users.pojo.AUsers;
import com.wangsh.userscenter.users.service.IUsersService;

/**
 * 登陆后的用户操作
 * 
 * url规划:凡是带/back的必须是登陆后才能操作
 * 
 * @author WangshSxt
 *
 */
@Controller
@RequestMapping("/back/users")
public class UsersBackController extends BaseController
{
	@Resource
	private IUsersService usersService;
	
	/*---- 用户模块开始 ----*/
	/**
	 * 用户列表
	 * @return
	 */
	@RequestMapping("/usersList")
	public String usersList(String keyword,String status,String st,String ed,Model model,
			String currentPage,String pageSize)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==usersList==");
		/* 将字符串变成时间
		 * */
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		
		/* 搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		
		Date stDate = null ; 
		Date edDate = null ;
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/*将字符串转换成日期
			 * 
			 * 完善了DateFormatUtil
			 * */
			stDate = this.dateFormatUtil.strDateTime(st); 
			edDate = this.dateFormatUtil.strDateTime(ed); ; 
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		PageInfoUtil pageInfoUtil = proccedPageInfo(currentPage, pageSize);
		
		/* 查询结果 */
		Map resultMap = this.usersService.findCondListUsersService(pageInfoUtil,condMap);
		List<AUsers> usersList = (List<AUsers>) resultMap.get("list");
		pageInfoUtil = (PageInfoUtil) resultMap.get("pageInfoUtil");
		/* 将结果放到四大作用域中 */
		model.addAttribute("usersList", usersList);
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 将查询条件放到request中 */
		model.addAttribute("keyword", keyword);
		model.addAttribute("status", status);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		return "/back/users/usersList";
	}
	
	/**
	 * 打开用户更新的页面
	 * 
	 * 查询你要更新的用户
	 * 
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(HttpServletRequest request,String operType , String self,String uid,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersUpdate--");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询用户 */
		condMap.put("id", uid);
		condMap.put("extend", "true");
		AUsers users = this.usersService.findOneUsersService(condMap);
		model.addAttribute("users", users);
		
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 查询所有启用的角色 */
			condMap.clear();
			condMap.put("status", "1");
			List<ARole> roleList = this.usersService.findCondListRoleService(null, condMap);
			model.addAttribute("roleList", roleList);
			return "/back/users/usersUpdate";
		}
		return "/back/users/usersInfo";
	}
	
	/**
	 * 用户更新提交
	 * @return
	 */
	@RequestMapping(value = "/usersUpdateSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String usersUpdateSubmit(String operType,String souPass,HttpServletRequest request,AUsers users)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersUpdateSubmit--");
		/* 根据id从数据库中再查询一次 
		 * 一一的赋值*/
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询用户 */
		condMap.put("id", users.getId());
		AUsers usersRes = this.usersService.findOneUsersService(condMap);
		
		boolean flag = false ; 
		if("updateInfo".equalsIgnoreCase(operType))
		{
			usersRes.setStatus(users.getStatus());
			flag = true ; 
		}
		
		if(flag)
		{
			usersRes.setUpdateTime(new Date());
			/* 千万千万别使用Spring的对象驱动;在Controller方法中增加一个对象 */
			JSONObject resDbJSON = this.usersService.updateOneUsersService(usersRes);
			this.info = resDbJSON.getString("info");
		}
		JSONObject resultJSON = this.printJuiJson(request, info);
		return resultJSON.toJSONString(); 
	}
	/*---- 用户模块结束 ----*/
}
